package com.tau.iplayer.client.dialogs;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gadgets.client.gwtrpc.GadgetsGwtRpc;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.tau.iplayer.client.DataStoreService;
import com.tau.iplayer.client.DataStoreServiceAsync;
import com.tau.iplayer.client.IPlayer;

public class DeletePlaylistDialog extends DialogBox {
	
	private static final String DELETE_MSG = "Are you sure you want to delete ";
	private static final boolean IS_AUTO_HIDE = false;
	private static final boolean IS_MODAL = true;
	private static final boolean IS_ANIMATED = true;
	private static final boolean IS_GLASS = true;
	private static final String HEADER = "Delete Playlist";
	
	private IPlayer iPlayer;
	
	public DeletePlaylistDialog(IPlayer iPlayer) {
		this.iPlayer = iPlayer;
		
		setAutoHideEnabled(IS_AUTO_HIDE);
		setModal(IS_MODAL);
		setAnimationEnabled(IS_ANIMATED);
		setGlassEnabled(IS_GLASS);
		setText(HEADER);
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		final Button deletePlaylistButton = new Button("OK");
		deletePlaylistButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				handleDeletePlaylistClicked();
			}
		});
		deletePlaylistButton.setStyleName("button");
		Button cancelButton = new Button("Cancel");
		cancelButton.setStyleName("button");
		cancelButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				handleCancelClick();
			}
		});
		horizontalPanel.add(deletePlaylistButton);
		horizontalPanel.add(cancelButton);
		horizontalPanel.setSpacing(3);
		
		HTML html = new HTML(DELETE_MSG + iPlayer.getCurrentPlaylist().getName() + "?", true);
		html.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		VerticalPanel panel = new VerticalPanel();
		panel.add(html);
		panel.add(horizontalPanel);
		panel.setCellHorizontalAlignment(horizontalPanel,
				HasHorizontalAlignment.ALIGN_CENTER);
		setWidth("200px");
		setWidget(panel);
	}

	private void handleCancelClick() {
		this.hide();
	}

	private void handleDeletePlaylistClicked() {
		DataStoreServiceAsync dataStoreService = GWT
		.create(DataStoreService.class);
		GadgetsGwtRpc.redirectThroughProxy((ServiceDefTarget) dataStoreService);
		dataStoreService.deletePlaylist(iPlayer.getViewer().getId(), iPlayer.getCurrentPlaylist(), new AsyncCallback<Void>() {
			
			@Override
			public void onSuccess(Void result) {
				iPlayer.loadPlaylists(iPlayer.getPreviousPlaylist());
				DeletePlaylistDialog.this.hide();
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}

}
